function trim(str){
    return rtrim(ltrim(str));
}

function ltrim(s){
    if(s == null) return "";
    var whitespace = new String(" \t\n\r");
    var str = new String(s);
    if (whitespace.indexOf(str.charAt(0)) != -1) {
        var j=0, i = str.length;
        while (j < i && whitespace.indexOf(str.charAt(j)) != -1){
            j++;
        }
        str = str.substring(j, i);
    }
    return str;
}

function rtrim(s){
    if(s == null) return "";
    var whitespace = new String(" \t\n\r");
    var str = new String(s);
    if (whitespace.indexOf(str.charAt(str.length-1)) != -1){
        var i = str.length - 1;
        while (i >= 0 && whitespace.indexOf(str.charAt(i)) != -1){
            i--;
        }
        str = str.substring(0, i+1);
    }
    return str;
}

function isEmpty (s) {
  if (s == null || trim(s) == "") return true;
  return false;
}

function isNotEmpty (s) {
	return !isEmpty(s);
}

function isInt(s,sign) {
	if (isEmpty(s)) return (arguments.length == 2 && sign == true);
	var re = new RegExp("^-?\\d+$");
	return re.test(s);
}

function isFloat(s,sign) {
	if (isEmpty(s)) return (arguments.length == 2 && sign == true);
	var re = new RegExp("^-?\\d+\\.?\\d*$");
	return re.test(s);
}

function isDigit(s,sign){
	if (isEmpty(s)) return (arguments.length == 2 && sign == true);
	var re = new RegExp("^-?\\d+\\.?$");
	return re.test(s);
}

function isChar(s,sign) {
	if (isEmpty(s)) return (arguments.length == 2 && sign == true);
	var re = new RegExp("^[A-Za-z0-9_]*$");
	return re.test(s);
}

function isIncludedChinese(s) {
	var re = new RegExp("[\u4e00-\u9fa5]+");
	return re.test(s);
}

function isMoney(s,sign){
	var strRef="";
	
	if(isEmpty(sign)) {
		strRef = "1234567890.";
	}else {
		strRef = "-1234567890.";
	}
		
	if(isEmpty(s)) return false;

	for (i=0;i<s.length;i++) {
  		var tempChar= s.substring(i,i+1);
  		if (strRef.indexOf(tempChar,0)==-1) {
   			return false; 
  		}
  		
	}
	var tempLen=s.indexOf(".");
   	if(tempLen!=-1){
    	var strLen=s.substring(tempLen+1,s.length);    		
    	if(strLen.length>2){ 
     		return false; 
    	}
   	}
   	
   var m = String(s);
   var dot=m.indexOf(".");
   var intMoney=m.substring(0,dot);
   if(intMoney.length >10 || String(s).length>=21){
      alert("位数过大，无法计算！");
   	  return false;
   }
   return true;
}

function isEmail(s,sign) {
	if (isEmpty(s)) return (arguments.length == 2 && sign == true);
  var emailPat=/^(.+)@(.+)$/;
  var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
  var validChars="\[^\\s" + specialChars + "\]";
  var quotedUser="(\"[^\"]*\")";
  var ipDomainPat=/^(\d{1,3})[.](\d{1,3})[.](\d{1,3})[.](\d{1,3})$/;
  var atom=validChars + '+';
  var word="(" + atom + "|" + quotedUser + ")";
  var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
  var domainPat=new RegExp("^" + atom + "(\\." + atom + ")*$");
  var matchArray=s.match(emailPat);
  if (matchArray == null)  return false;
  var user=matchArray[1];
  var domain=matchArray[2];
  if (user.match(userPat) == null)   return false;
  var IPArray = domain.match(ipDomainPat);
  if (IPArray != null) {
    for (var i = 1; i <= 4; i++) {
      if (IPArray[i] > 255) return false;
    }
    return true;
  }
  var domainArray=domain.match(domainPat);
  if (domainArray == null)  return false;
  var atomPat=new RegExp(atom,"g");
  var domArr=domain.match(atomPat);
  var len=domArr.length;
  if ((domArr[domArr.length-1].length < 2) || (domArr[domArr.length-1].length > 3)) {
    return false;
  }
  if (len < 2) return false;
  return true;
}                     

function isEqual(one,two){
	if (!isFloat(one) && !isFloat(two)) return false;
	var p1 = parseFloat(one);
	var p2 = parseFloat(two);
	return (p1 == p2);
}

function isGreaterThan(one,two){
	if (!isFloat(one) && !isFloat(two)) return false;
	var p1 = parseFloat(one);
	var p2 = parseFloat(two);
	return (p1 > p2);
}

function isGreaterEqual(one,two){
	if (!isFloat(one) && !isFloat(two)) return false;
	var p1 = parseFloat(one);
	var p2 = parseFloat(two);
	return (p1 >= p2);
}

function formatMoney(s,fraction,sign){

   var strRef="";
	
  if(isEmpty(sign)) {
	 strRef = "1234567890.";
  }else {
	 strRef = "-1234567890.";
  }
	
  if(!fraction) fraction=2;
  if(isEmpty(s)) {    
    return '';
  }
  
  for (i=0;i<s.length;i++) {
    tempChar= s.substring(i,i+1);
    if (strRef.indexOf(tempChar,0)==-1) {
    	alert('金额:'+s+',格式不正确,请检查');
      	return ''; 
    }
  }
  
  var muti=1;
  for(var i=1;i<=fraction;i++){
  	muti=muti*10;
  }
  money=''+Math.round(parseFloat(s)*muti)*1.0/(muti);
  tempLen=money.indexOf(".");
  if(tempLen!=-1){
    strLen=money.substring(tempLen+1,money.length);     
    for(j=strLen.length;j<fraction;j++){ 
     	money=''+money+'0'; 
     }
     return money;
  }
  else{  
    return ''+money+'.00';
  }
}
  
function strlen(str)
{
	var len;
	var i;
	len = 0;
	for (i=0;i<str.length;i++)
	{
		if (str.charCodeAt(i)>255) len+=2; else len++;
	}
	return len;
}
